using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Lizk.WebScraper
{
    
    [XmlRoot("ScraperResult")]
    [Serializable]
    public class ScraperResult
    {
        protected string id = "";
        protected string title  ="";
        protected string imageUrl = "";
        protected string description = "";
        protected int year;
        private string summary= "";
        private string tagline = "";

        public string TagLine
        {
            get { return tagline; }
            set { tagline = value; }
        }

        private TimeSpan runtime = TimeSpan.Zero;

        [XmlIgnore]
        public TimeSpan Runtime
        {
            get { return runtime; }
            set { runtime = value; }
        }

        public int RuntimeInMinutes
        {
            get { return (int)runtime.TotalMinutes; }
            set { runtime = new TimeSpan(0, value, 0); }
        }

        private float rating;

        public float Rating
        {
            get { return rating; }
            set { rating = value; }
        }

        private int votes;

        public int Votes
        {
            get { return votes; }
            set { votes = value; }
        }


        private List<string> genres = new List<string>();

        public List<string> Genres
        {
            get { return genres; }
        }

        private List<Episode> episodes = new List<Episode>();

        public List<Episode> Episodes
        {
            get { return episodes; }
        }
        private List<Person> people = new List<Person>();

        public List<Person> People
        {
            get { return people; }
        }

        public int Year
        {
            get { return year; }
            set { year = value; }
        }
        public string ID
        {
            get { return id; }
        }

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public string ImageUrl
        {
            get { return imageUrl; }
            set { imageUrl = value; }
        }

        public ScraperResult() { }

        public ScraperResult(string id)
        {
            this.id = id;
        }

        [XmlIgnore]
        public virtual string ShortDescription
        {
            get
            {
                return title + (year > 0 ? " (" + year + ")" : "");
            }
        }
        [XmlIgnore]
        public virtual string Description
        {
            get
            {
                return ShortDescription + Environment.NewLine + Summary;
            }
        }

        public string Summary
        {
            get { return summary; }
            set { summary = value; }
        }

        public static ScraperResult FromFile(string xmlFile)
        {
            // Construct an instance of the XmlSerializer with the type
            // of object that is being deserialized.
            XmlSerializer mySerializer = new XmlSerializer(Scraper.ResultTypes[0], Scraper.ResultTypes.ToArray());
            // To read the file, create a FileStream.
            FileStream myFileStream = new FileStream(xmlFile, FileMode.Open);
            // Call the Deserialize method and cast to the object type.
            ScraperResult o = (ScraperResult)mySerializer.Deserialize(myFileStream);
            myFileStream.Close();
            return o;
        }

        public override string ToString()
        {
            return ShortDescription;
        }

        public static void SaveResult(ScraperResult sr, string xmlFile)
        {
            XmlSerializer mySerializer = new XmlSerializer(sr.GetType(), Scraper.ResultTypes.ToArray());
            // To write to a file, create a StreamWriter object.
            StreamWriter myWriter = new StreamWriter(xmlFile);
            mySerializer.Serialize(myWriter, sr);
            myWriter.Close();
        }

        [XmlInclude(typeof(ScraperResult))]
        private class ResultContainer
        {
            private ScraperResult result;

            public ScraperResult Result
            {
                get { return result; }
                set { result = value; }
            }

            public ResultContainer(ScraperResult result)
            {
                this.result = result;
            }

            public ResultContainer()
            {
            }
        }

        public Episode GetEpisode(int season, int episode)
        {
            foreach (Episode e in Episodes)
            {
                if (e.Season == season && e.EpisodeNumber == episode)
                    return e;
            }
            return null;
        }

    }

    [XmlRoot]
    [Serializable]
    public class Episode
    {
        private string title = "";

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        private string id = "";

        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        public Episode() { }

        public Episode(string title)
        {
            this.title = title;
        }
        private DateTime airDate;

        public DateTime AirDate
        {
            get { return airDate; }
            set { airDate = value; }
        }

        private string summary = "";

        public string Summary
        {
            get { return summary; }
            set { summary = value; }
        }


        public override string ToString()
        {
            return season + "x" + episode + ": " + title + "(" + airDate.ToShortDateString() + ")" + Environment.NewLine + "\t" + summary;
        }
        private int season;

        public int Season
        {
            get { return season; }
            set { season = value; }
        }
        private int episode;

        public int EpisodeNumber
        {
            get { return episode; }
            set { episode = value; }
        }

    }

    [XmlRoot]
    [Serializable]
    public class Person
    {
        private string id = "";

        public string ID
        {
            get { return id; }
            set { id = value; }
        }

        private string name  = "";

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string role  ="";

        public string Role
        {
            get { return role; }
            set { role = value; }
        }

        private string job = "";

        public string Job
        {
            get { return job; }
            set { job = value; }
        }

        public Person(string id, string name)
        {
            this.name = name;
            this.id = id;
        }
        public Person() { }
    }


}
